<template>
	<view class="member" v-if="combo_list.length > 0">
		<view class="head_bg">
			<u-navbar leftIconColor="#ffffff" :placeholder="true" bgColor="rgba(0,0,0,0.0)" :autoBack="true">
				<view slot="center" style="color: #fff;">
					会员中心
				</view>
			</u-navbar>
			<view class="member-content">
				<view class="member-box" v-if="member == ''">
					<view class="member-title">
						开通享专属特权
					</view>
					<view class="member-text">
						尊享豪华 专属特权
					</view>
				</view>
				<view class="member-box" v-else>
					<view class="member-title" v-if="member.level == 1">
						周卡会员
					</view>
					<view class="member-title" v-if="member.level == 2">
						月卡会员
					</view>
					<view class="member-text">
						到期时间:{{$u.timeFormat(member.expiretime, 'yyyy-mm-dd hh:MM:ss')}}
					</view>
				</view>
			</view>
			<view class="accessories_content">
				<view v-if="member == ''">
					开通VIP即可尊享专属特权
				</view>
				<view v-else>
					您已开通会员
				</view>
			</view>
		</view>
		<view>
			<view class="combo">
				<view class="thali-title">会员套餐</view>
				<view class="list">
					<view class="item" :class="{'is_active':item.default == activeId}"
						v-for="(item,index) in combo_list" :key="index" @click="checkvip(item.default)">
						<view class="item_time">{{item.title}}</view>
						<view class="item_money">
							<image src="https://applogin.panpanzuyou.com/tp/static/vip/diamond.png" mode=""></image>
							<text style="font-size: 32rpx;">{{Number(item.price)}}</text>
						</view>
						<view class="item_old">{{item.days}}天</view>
					</view>
				</view>
			</view>
			<view class="tequan-box">
				<view class="combo-title">
					<view>
						会员说明
					</view>
					<view class="combo-text">
						专属特权，等你来领
					</view>
				</view>
				<view class="explain">
					<view class="explain-title">
						周卡会员：
					</view>
					<view class="explain-text">
						一周内可发布组局
					</view>
					<view class="explain-title">
						月卡会员：
					</view>
					<view class="explain-text">
						一个月内可发布组局
					</view>
				</view>
			</view>
		</view>
		<u-action-sheet :actions="pay_list" :show="payWay" @select="handleClickPayWay"></u-action-sheet>
		<view class="btn" @click="gobuy">
			{{member != ''?"立即续费":"立即开通"}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: false,
				pay_list: [{
						name: '微信支付'
					},
					{
						name: '余额支付'
					},
				],
				background: {
					backgroundColor: '#1c1c1c',
				},
				payWay: false, //支付方式弹窗
				combo_list: [],
				activeId: 1,
				member: null
			}
		},
		onShow() {
			this.getMemberInfo()
			this.vipinfo()
		},
		methods: {
			vipinfo() {
				this.$Request({
					method: 'POST',
					url: '/api/vip/getUserVip',
					data: {},
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.member = res.data
					}
				})
			},
			checkvip(id) {
				this.activeId = id
			},
			gobuy() {
				this.payWay = true
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e) {
				this.checked = e.value
			},
			govip() {
				uni.navigateTo({
					url: '/pageuser/user/vipxy'
				})
			},
			getMemberInfo() {
				this.$Request({
					method: 'POST',
					url: '/api/vip/viplist',
					data: {},
					ismsg: false,
				}).then(res => {
					console.log(res.data, 1233)
					if (res.code == 1) {
						this.combo_list = res.data[0].pricedata

					}
				})
			},
			handleClickPayWay(e) {
				uni.showLoading({
					mask: true
				})
				let app = this
				app.payWay = false
				if (e.name == '微信支付') {
					let params = {
						level: '',
						days: '',
						paytype: 'wechat'
					}
					if (app.activeId == 1) {
						params.level = 1
						params.days = 7
					}
					if (app.activeId == 0) {
						params.level = 1
						params.days = 30
					}
					app.$Request({
						method: 'POST',
						url: '/api/vip/submit',
						data: params,
						ismsg: false,
					}).then(res => {
						uni.hideLoading()
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.timeStamp,
							nonceStr: res.nonceStr,
							package: res.package,
							signType: res.signType,
							paySign: res.paySign,
							success: function(res) {
								uni.showToast({
									title: '支付成功！',
									icon: 'none'
								})
								app.getMemberInfo()
								app.vipinfo()
							},
							fail: function(err) {
								uni.showToast({
									title: '支付失败！',
									icon: 'none'
								})
								console.error('支付失败', err);
								// 处理支付失败的逻辑
							}
						});
					})
				}
				if (e.name == '余额支付') {
					let params = {
						level: '',
						days: '',
						paytype: 'balance'
					}
					if (app.activeId == 1) {
						params.level = 1
						params.days = 7
					}
					if (app.activeId == 0) {
						params.level = 1
						params.days = 30
					}
					app.$Request({
						method: 'POST',
						url: '/api/vip/submit',
						data: params,
						ismsg: true,
					}).then(res => {
						uni.hideLoading()
						console.log(res)
						app.getMemberInfo()
						app.vipinfo()
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.member {
		min-height: 100vh;
		position: relative;
	}

	.head_bg {
		width: 100%;
		background: url('https://applogin.panpanzuyou.com/tp/static/vip/vip-bg.png') no-repeat;
		background-size: 100% 445rpx;
		height: 445rpx;
		position: relative;

		.member-content {
			position: absolute;
			top: 53%;
			left: 9%;

			.member-box {
				color: #8E7658;

				.member-title {
					font-size: 36rpx;
					font-weight: 600;
				}

				.member-text {
					font-size: 24rpx;
					margin-top: 10rpx;
				}
			}
		}

		.accessories_content {
			width: 422rpx;
			position: absolute;
			bottom: 25rpx;
			left: 0;
			right: 0;
			margin: 0 auto;
			text-align: center;
			padding: 15rpx 0;
			border-radius: 30rpx;
			background: linear-gradient(90deg, #1D1F25 0%, #474747 100%);
			box-shadow: 0rpx 3rpx 16rpx 1rpx rgba(0, 0, 0, 0.49);
			color: #FAE2C3;
			font-size: 28rpx;
		}
	}

	.combo {
		background-color: #fff;

		.thali-title {
			margin-bottom: 30rpx;
			text-align: center;
			color: #333333;
			font-size: 34rpx;
			font-weight: 900;
			padding-top: 20rpx;
		}

		.list {
			padding: 0 30upx;
			display: grid;
			grid-template-columns: repeat(4, 1fr);
			grid-row-gap: 20upx;
			grid-column-gap: 20upx;
			margin-bottom: 30rpx;

			.item {
				border: 3px solid #EDD18C;
				border-radius: 20rpx;
				position: relative;
				text-align: center;
				padding: 20rpx 0;

				.item_hot {
					position: absolute;
					right: 0;
					top: -17rpx;
					padding: 4upx 15upx;
					border-radius: 0 20upx 0 20upx;
					background: linear-gradient(90deg, #FF4D2F 0%, #FF8329 100%);
					font-size: 22rpx;
					color: #FFFFFF;
				}

				.item_time {
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 800;
					color: #333333;
					line-height: 36rpx;
					margin-top: 10rpx;
				}

				.item_money {
					font-size: 38rpx;
					font-family: PingFang SC;
					font-weight: 800;
					color: #FD5350;
					line-height: 48rpx;
					padding: 10rpx;

					display: flex;
					justify-content: center;
					align-items: center;

					image {
						width: 40rpx;
						height: 40rpx;
					}
				}

				.item_old {
					font-size: 22rpx;
					font-family: PingFang SC;
					font-weight: 200;
					color: #5B544A;
					line-height: 30rpx;
				}

				.item_discount {
					width: 116rpx;
					height: 29rpx;
					background: #FF502F;
					border-radius: 15rpx;
					position: absolute;
					right: -10rpx;
					top: -20rpx;

					.discount_txt {
						font-size: 20rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #F8F1DF;
						line-height: 36rpx;
					}
				}
			}

			.is_active {
				background: rgba(244, 216, 145, 0.3);
				border: 3px solid #EDD18C;

				.item_time {
					color: #644931;
				}

				.item_money {
					color: #FF502F;
				}

				.item_discount {
					background: #FF4F2E;

					.discount_txt {
						color: #FFFFFF;
					}
				}
			}
		}
	}

	.privilege {
		position: relative;
		margin-top: 53upx;

		.privilege_line {
			position: absolute;
			width: 436upx;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
		}

		.privilege_icon {
			width: 63upx;
			margin-right: 10upx;
		}

		.privilege_txt {
			font-size: 35upx;
			font-family: Microsoft YaHei;
			font-weight: bold;
			color: #9f6f36;
		}
	}

	.combo-title {
		font-size: 34rpx;
		font-weight: 900;
		color: #333333;
		text-align: center;
		padding: 30rpx 30rpx 0 30rpx;
		border-top: 1rpx solid rgba(204, 204, 204, 0.5);
	}

	.combo-text {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
		text-align: center;
	}

	.vip-tips {
		height: 26rpx;
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #575757;
		line-height: 36rpx;
		padding: 0 30rpx;
		display: flex;
		align-items: center;

		image {
			width: 30rpx;
			height: 30rpx;
			margin-right: 20rpx;
		}
	}

	.explain {
		margin-top: 20rpx;
		padding-bottom: 100rpx;

		.explain-title {
			font-size: 28rpx;
			color: #333333;
			margin-bottom: 10rpx;
			font-weight: 900;
		}

		.explain-text {
			font-size: 26rpx;
			color: #666666;
			margin-bottom: 10rpx;
		}
	}

	.tequan-box {
		background-color: #fff;
		padding: 30rpx;
	}

	.btn {
		width: 645rpx;
		height: 80rpx;
		background: #F4D891;
		border-radius: 52rpx;
		font-size: 28rpx;
		color: #773B00;
		position: fixed;
		bottom: 20rpx;
		left: 0;
		right: 0;
		margin: 0 auto;
		text-align: center;
		line-height: 80rpx;
	}
</style>